package left42dead;

import java.util.List;

/**
 * Character abstract class that represents a character
 */
public abstract class Character
{
    protected int healthPoints;        // Represents the health
    protected int initialHealthPoints; // The initial health of this character
    protected Location location;       // Location of the character
    protected Field field;             // Field that the character is onto

    /**
     * Constructor of Character class.
     * @param healthPoints initial HP
     * @param field Field where he's located
     * @param location The location in the field where he's located
     */
    public Character (int healthPoints, Field field, Location location)
    {
        this.healthPoints = healthPoints;
        this.initialHealthPoints = healthPoints;
        this.field = field;
        setLocation(location);
    }

    public int getHealthPoints ()
    {
        return healthPoints;
    }

    /**
     * Decrease the number of HP by a certain amount. HP cannot go below 0.
     * @param reduction number of HP to reduce
     */
    public void reduceHealthPoints (int reduction)
    {
        healthPoints = healthPoints - reduction;

        if (healthPoints < 0)
            healthPoints = 0;
    }

    /**
     * Increase the number of HP by a certain amount. HP cannot go up initial
     * health
     * @param augmentation number of HP
     */
    public void increaseHealthPoints (int augmentation)
    {
        healthPoints = healthPoints + augmentation;

        if (healthPoints > initialHealthPoints)
            healthPoints = initialHealthPoints;
    }

    /**
     * Return true if the chracter is dead
     * @return true if he's dead, false otherwise
     */
    public boolean isDead ()
    {
        return (getHealthPoints() <= 0);
    }

    /**
     * Kill the character
     */
    public void setDead ()
    {
        healthPoints = 0;
    }

    /**
     * Abstract method to run a character turn in the simulator
     * @param newCharacters List of newly born characters
     */
    public abstract void run (List<Character> newCharacters);

    /**
     * Method triggered on each character at the end of each turn.
     */
    public void endOfTurn () {}

    // Accessors and getters
    public void setLocation (Location newLocation)
    {
        if (location != null)
            field.clear(location);

        location = newLocation;
        field.place(this, location);
    }

    public Location getLocation ()
    {
        return location;
    }

    public Field getField ()
    {
        return field;
    }

    public void setField (Field field)
    {
        this.field = field;
    }
}
